<!DOCTYPE HTML>
<link rel="help" href="http://url.spec.whatwg.org/#dom-url">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
function assert_type_error(func, expected) {
    try {
        func();
    } catch (ex) {
        assert_true(ex instanceof TypeError);
        assert_equals(String(ex), expected);
        return;
    }
    assert_true(false, 'Expected a TypeError exception with string: ' + expected);
}

test(function() {
    assert_equals(new URL('http://www.domain.com/a/b').toString(), 'http://www.domain.com/a/b');
    assert_equals(new URL('/c/d', 'http://www.domain.com/a/b').toString(), 'http://www.domain.com/c/d');
    assert_equals(new URL('b/c', 'http://www.domain.com/a/b').toString(), 'http://www.domain.com/a/b/c');

    var base = new URL('http://www.domain.com/a/b');
    assert_equals(new URL('b/c', base).toString(), 'http://www.domain.com/a/b/c');

    // Unmatched surrogate handling
    var encodedReplacementCharacter = encodeURIComponent('\ufffd');
    assert_equals(new URL('b/c', 'http://www.domain.com/\ud801/b').toString(), 'http://www.domain.com/' + encodedReplacementCharacter + '/b/c');
}, 'Valid URLs');

test(function() {
    assert_type_error(
        function() { new URL(); },
        'TypeError: Failed to construct \'URL\': 1 argument required, but only 0 present.');
    assert_type_error(
        function() { new URL(''); },
        'TypeError: Failed to construct \'URL\': Invalid URL');
    assert_type_error(
        function() { new URL('','about:blank'); },
        'TypeError: Failed to construct \'URL\': Invalid URL');
    assert_type_error(
        function() { new URL('abc'); },
        'TypeError: Failed to construct \'URL\': Invalid URL');
    assert_type_error(
        function() { new URL('//abc'); },
        'TypeError: Failed to construct \'URL\': Invalid URL');
    assert_type_error(
        function() { new URL('http:///www.domain.com/', 'abc'); },
        'TypeError: Failed to construct \'URL\': Invalid base URL');
    assert_type_error(
        function() { new URL('http:///www.domain.com/', null); },
        'TypeError: Failed to construct \'URL\': Invalid base URL');
    assert_type_error(
        function() { new URL('//abc', null); },
        'TypeError: Failed to construct \'URL\': Invalid base URL');
}, 'Invalid URL parameters');

test(function() {
    function assert_enumerable(p) {
        assert_true(p in URL.prototype);
        assert_true(URL.prototype.propertyIsEnumerable(p));
    }

    assert_true('URL' in self);

    // TODO: uncomment when implemented.
    // assert_true('domainToASCII' in URL);
    // assert_true('domainToUnicode' in URL);

    // Arguably better failure stacks to spell them out this way..
    assert_enumerable('toString');
    assert_enumerable('origin');
    assert_enumerable('protocol');
    assert_enumerable('username');
    assert_enumerable('password');
    assert_enumerable('host');
    assert_enumerable('hostname');
    assert_enumerable('port');
    assert_enumerable('pathname');
    assert_enumerable('search');
    assert_enumerable('searchParams');
    assert_enumerable('hash');
}, 'URL interface');
</script>
